import { _decorator, Button, Label, Node, Sprite } from 'cc';
import { UIComponent } from '../../../Framework/Scripts/Managers/UIComponent';
import { AbNames, EventMsgKes } from '../Config/ResConfig';
import { WECHAT } from 'cc/env';
import { Platform } from '../../../Framework/Scripts/Platform/PlatformWX';
import { GameConfig } from '../Config/GameCfg';
import { GameUtils } from '../../../Framework/Scripts/Utils/GameUtils';
import { LoginBackRes } from '../../AssetPackage/Props/Login/Login';
import { GameLoadingMsg } from './GameLoadingMsg';
import { GameHallMsg } from '../GameHall/GamHall/GameHallMsg';
import { ViewMgr } from '../../../Framework/Scripts/Managers/ViewMgr';
import { CocosUtils } from '../../../Framework/Scripts/Utils/CocosUtils';
import { UIMgr, UIType } from '../../../Framework/Scripts/Managers/UIMgr';
import { SceneMgr } from '../../../Framework/Scripts/Managers/SceneMgr';
const { ccclass, property } = _decorator;

@ccclass('GameLoading_Ctr')
export class GameLoading_Ctr extends UIComponent {
    spLoading: Sprite;
    btnStart: Node;
    spload: Node;
    isClick: boolean;
    nLab: Node;
    testUid: string;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();

        const urlParams = GameUtils.getUrlParams();
        console.log('URL 参数:', urlParams);

        this.testUid = urlParams.uid;
        console.log('=====testUid=======',this.testUid);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.node.addComponent(GameLoadingMsg);
        this.registerEvent();

        this.spLoading = this.ViewComponent('spLoadingBg/spLoading',Sprite);
        this.btnStart = this.ViewNode('btnStart');
        this.spload = this.ViewNode('spLoadingBg');
        this.nLab = this.ViewNode('Label');
        this.AddButtonListener('btnStart',this,this.onBtnStartClick);
    }

    private InitData(){
        this.setBtnStatus(false);
        this.spLoading.fillRange = 0;
        this.isClick = true;

        this.spload.active = false;
        this.btnStart.active = false;
        this.nLab.active = false;
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.loading,this.onLoadingPro,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.loadingEnd,this.onLoadingProEnd,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgLoginRes,this.onMsgLoginRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.loading,this.onLoadingPro,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.loadingEnd,this.onLoadingProEnd,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgLoginRes,this.onMsgLoginRes,this);
    }

    update(deltaTime: number) {
        
    }

    //游客登录随机账号数据
    private getGuest(){
        let user = {
            id: 'web_' + GameUtils.generateRandomString(10),
            name: GameUtils.generateRandomName(),
            avatar: GameUtils.generateRandomAvatar()
        }
        return user;
    }

    //点击登录
    private onBtnStartClick(btn:Button){
        if(this.isClick == false){
            return;
        }
        this.isClick = false;

        let isUseWs = GameConfig.isUseWs;
        if(isUseWs && WECHAT){
            Platform.inst.LoginWX();
        }else{
            this.testUid = 'web_0i7rqFGjsq';
            // 发送登录消息
            let data = this.getGuest();
            if(this.testUid){
                data.id = this.testUid;
            }
            globalThis.gameMgr._GameLoadingMsg.LoginRes(data.id,data.name,data.avatar);
        }
    }

    private setBtnStatus(isStart: boolean){
        if(isStart){
            this.btnStart.active = false;
            this.spload.active = true;
        }else{
            this.btnStart.active = true;
            this.spload.active = false;
        }
    }

    private onLoadingPro(data: any){
        this.spLoading.fillRange = data;
    
        this.spload.active = true;
        this.btnStart.active = false;
        this.nLab.active = true;
        this.nLab.getComponent(Label).string = "正在加载资源中....";
    }
    
    //加载大厅结束
    private onLoadingProEnd(){
        this.spload.active = false;
        this.btnStart.active = true;
        this.nLab.active = false;
    }

    private onMsgLoginRes(data: LoginBackRes){
        console.log('=========onMsgLoginRes==========');
        this.setBtnStatus(true);
        this.isClick = true;

        globalThis.gameMgr._GameLoadingMsg.destroy();

        if(this.hasProInObj(data,'hallNet')){
            //链接大厅的网络
            const callFunction = SceneMgr.inst.SetCallFun(SceneMgr.inst.showHallViwe);
            let call ={
                func: callFunction,
                path: "Prefabs/GameHall"
            }
            ViewMgr.inst.ConnectToHall(call);
        }else{
            console.error('登录消息返回没有hallNet字段');
        }
    }
}